package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.api.socket.WSMessageService;
import cn.wolfcode.trip.base.domain.ChatRecord;
import cn.wolfcode.trip.base.util.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/message")
public class WebSocketController {
    private Logger logger = LoggerFactory.getLogger(WebSocketController.class);

    //websocket服务层调用类
    @Autowired
    private WSMessageService wsMessageService;

    //请求入口
    @RequestMapping(value = "/senderId/{sender.id}/receiverId/{receiver.id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult TestWebSocket(ChatRecord chatRecord) {
        JsonResult jsonResult = new JsonResult();
        logger.debug("收到发送请求，向用户{}的消息：{}", chatRecord.getReceiver().getId(), chatRecord.getContent());
        if (wsMessageService.sendToAllTerminal(chatRecord)) {
            return jsonResult;
        } else {
            jsonResult.mark("发送失败");
            return jsonResult;
        }
    }
}